<?php //$Rev:: 33         $ // DO NOT REMOVE

error_reporting(E_ALL);

/**
 * Buddy class (Buddy is all TOC buddylist management utils.)
 * 
 * @author Andrew Heebner <andrew@evilwalrus.com> 
 * @version $Revision: 33 $
 * @require PHP 5.0
 */
class Buddy
{

	public $core;
    public $autorespond = array();

	/**
	 * __construct: Buddy constructor
	 * 
	 * @param object $object Object to store for parent referencing
	 * @access public 
	 * @return void 
	 */
	public function __construct(ATAC $object)
	{
		$this->core = $object;
	} 

	/**
	 * __destruct: Buddy destructor
	 * 
	 * @access public 
	 * @return void 
	 */
	public function __destruct()
	{
		unset($this->core);
	} 

	/**
	 * __call: Buddy invalid method parser
	 * 
	 * @param string $method Invalid method called
	 * @access private 
	 * @return void 
	 * @throws ATACException
	 */
	public function __call($method, $args)
	{
		throw new ATACException('Invalid method called => (' . __CLASS__ . '::' . $method . ')');
	}

	/**
	 * aim_list_mode: Changes the buddylist mode
	 * 
	 * @param integer $mode Mode to set buddylist to
	 * @access public 
	 * @return void 
	 * @proto void aim_list_mode(integer mode)
	 */
	public function aim_list_mode($mode)
	{
        switch($mode) {
            case LIST_PERMIT_ALL:
            case LIST_DENY_ALL:
            case LIST_PERMIT_SOME:
            case LIST_DENY_SOME:
            case LIST_ALLOW_BUDDYLIST:
            case LIST_BLOCK_AIM:
                $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc2_set_pdmode ' . $mode);
                break;
            default:
                $this->core->aim_debug(sprintf('Supplied mode "\%s" to "%s" is not a valid buddylist mode', $mode, __FUNCTION__), AIM_WARN);
                break;
        }
	}

	/**
	 * aim_update_buddy: Update buddy status on buddylist (permit/deny) and pounce modes
	 *
     * @param string $buddy Buddy to update
	 * @param integer $mode Mode to set buddy to
	 * @access public 
	 * @return void 
	 * @proto void aim_update_buddy(string buddy, integer mode)
	 */
	public function aim_update_buddy($buddy, $mode)
	{
        $buddy = $this->core->aim_normalize($buddy);
        switch($mode) {
            case ADD_PERMIT:
                $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc2_add_permit ' . $this->core->aim_normalize($buddy));
                break;
            case REMOVE_PERMIT:
                $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc2_remove_permit ' . $this->core->aim_normalize($buddy));
                break;
            case ADD_DENY:
                $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc2_add_deny ' . $this->core->aim_normalize($buddy));
                break;
            case REMOVE_DENY:
                $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc2_remove_deny ' . $this->core->aim_normalize($buddy));
                break;
            default:
                $this->core->aim_debug(sprintf('Supplied mode "\%s" to "%s" is not a valid buddy mode', $mode, __FUNCTION__), AIM_WARN);
                break;
        }
	} 

	/**
	 * aim_add_buddy: Add a buddy to the buddylist
	 * 
	 * @param string $buddy Buddy to add to buddylist
	 * @param string $group (optional) Group to add Buddy to
	 * @access public 
	 * @return boolean 
	 * @proto boolean aim_add_buddy(string buddy[, string group])
	 */
	public function aim_add_buddy($buddy, $group = 'Buddies')
	{
		if (!empty($buddy) && $buddy != '' && !is_null($buddy)) {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf("toc2_new_buddies \"g:%s\nb:%s\n\"", $group, $this->core->aim_normalize($buddy)));
            return true;
		}
        return false;
	} 

	/**
	 * aim_remove_buddy: Remove buddies from buddylist
	 * 
	 * @param string $buddy Buddy to add to buddylist
     * @param string $group (optional) Group to remove Buddy from
	 * @access public 
	 * @return boolean 
	 * @proto boolean aim_remove_buddy(string buddy[, string group])
	 */
	public function aim_remove_buddy($buddy, $group = 'Buddies')
	{
		if (!empty($buddy) && $buddy != '' && !is_null($buddy)) {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc2_remove_buddy %s %s', $group, $this->core->aim_normalize($buddy)));
            return true;
		}
        return false;
	}

	/**
	 * aim_get_user_class: Builds user class array frm signon information
	 * 
     * @param string $class Buddy class to parse for information
	 * @access private
	 * @return array 
	 * @proto array aim_get_user_class(string class)
	 */
    public function aim_get_user_class($class)
    {
        // fill the array with defaults
        $data = array('aol'   =>  false,
                      'type'  =>  '',
                      'away'  =>  false);

        // get the user information now
        switch (strlen($class)) {
            case 2:
                // user is (not) an AOL user
                switch ($class{0}) {
                    case ' ':   break;
                    case 'A':   $data['aol'] = true;    break;
                }
                // user type
                switch ($class{1}) {
                    case ' ':   break;
                    case 'A':   $data['type'] = USER_TYPE_ADMIN;        break;
                    case 'U':   $data['type'] = USER_TYPE_UNCONFIRMED;  break;
                    case 'O':   $data['type'] = USER_TYPE_NORMAL;       break;
                }
                break;
            case 3:
                // user is away or on ignore
                switch ($class{0}) {
                    case ' ':   break;
                    case 'A':   $data['aol'] = true;    break;
                }
                // user type
                switch ($class{1}) {
                    case ' ':   break;
                    case 'A':   $data['type'] = USER_TYPE_ADMIN;        break;
                    case 'U':   $data['type'] = USER_TYPE_UNCONFIRMED;  break;
                    case 'O':   $data['type'] = USER_TYPE_NORMAL;       break;
                }
                // user status (away)
                switch ($class{2}) {
                    case '\0':  break;
                    case ' ':   break;
                    case 'U':   $data['away'] = true;   break;
                }
                break;
        }
        return $data;   
    }

    public function aim_parse_config($config)
    {
        $data = explode("\n", $config);

        $group = -1;
        $buddy = array();

        foreach ($data as $line) {
            $bi = explode(':', $line);
            switch ($bi[0]) {
                case 'g':
                    $buddy[trim($bi[1])] = array();
                    $group++;
                    break;
                case 'b':
                    $key = key(array_slice($buddy, $group, 1, true));
                    $buddy[$key][] = trim($bi[1]);
                    break;
            }
        }
        // copy the (main) buddylist to the core
        $this->core->buddylist = $buddy['Buddies'];
    }

    public function aim_is_buddy($buddy, $group = 'Buddies')
    {
        return in_array($this->core->aim_normalize($buddy), $this->core->buddylist);
    }


    public function aim_add_autorespond($buddy, $message = '')
    {
        $buddy = $this->core->aim_normalize($buddy);
        $message = (empty($message)) ? 'Sorry, I\'m not accepting messages from you right now.' : $message;
        if (!array_key_exists($buddy, $this->autorespond) && !$this->core->utilities->aim_is_admin($buddy) && ($buddy != $this->core->user['screenname'])) {
            $this->autorespond[$buddy] = $message;
        }
    }

    public function aim_remove_autorespond($buddy)
    {
        $buddy = $this->core->aim_normalize($buddy);
        if (array_key_exists($buddy, $this->autorespond) && !$this->core->utilities->aim_is_admin($buddy) && ($buddy != $this->core->user['screenname'])) {
            unset($this->autorespond[$buddy]);
        }
    }

} // end Buddy

?>